
using System;
using System.Collections.Generic;
using System.Text;
using SEAM.Core;

namespace SEAM.Data
{
    /// <summary>
    /// Summary description for DBUtility.
    /// </summary>
    public class DBUtility
    {
        public static string Fix(object Input)
        {
            if (Input == null)
                return "null";
            else if (Input is string || Input is DateTime)
                return "'" + Input.ToString().Replace("'", "''") + "'";
            else
                return Input.ToString();
        }

        public static byte? GetByte(object Input)
        {
            return GetByte(Input, null);
        }

        public static byte? GetByte(object Input, byte? DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToByte(Input);
        }

        public static short? GetShort(object Input)
        {
            return GetShort(Input, null);
        }

        public static short? GetShort(object Input, short? DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToInt16(Input);
        }

        public static int? GetInteger(object Input)
        {
            return GetInteger(Input, null);
        }

        public static int? GetInteger(object Input, int? DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToInt32(Input);
        }

        public static long? GetLong(object Input)
        {
            return GetLong(Input, null);
        }

        public static long? GetLong(object Input, long? DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToInt64(Input);
        }

        public static string GetString(object Input)
        {
            return GetString(Input, null);
        }
        
        public static string GetString(object Input, string DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToString(Input);
        }

        public static bool? GetBool(object Input)
        {
            return GetBool(Input, null);
        }

        public static bool? GetBool(object Input, bool? DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToBoolean(Input);
        }
        
        public static DateTime? GetDateTime(object Input)
        {
            return GetDateTime(Input, null);
        }

        public static DateTime? GetDateTime(object Input, DateTime? DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToDateTime(Input);
        }

        public static float? GetFloat(object Input)
        {
            return GetFloat(Input, null);
        }

        public static float? GetFloat(object Input, float? DefaultVal)
        {
            if (Input == DBNull.Value)
                return DefaultVal;
            else
                return Convert.ToSingle(Input);
        }

    }
}
